angular.module('meHistoryOrderDelcontrollers', [])

  .controller('MeHistoryOrderDelCtrl', function ($scope, $ionicActionSheet, $state, $timeout, $ionicPopup, Storage, MeHistoryOrderDel,History,Loading,Toast, ENV) {

    var memberId ;
    $scope.ENV = ENV;


    //进入界面时，加载历史酒单详情数据
    $scope.$on('$ionicView.beforeEnter', function () {
      memberId = Storage.get("member").member_id;
      $scope.mrw_id = MeHistoryOrderDel.getMrwId();
      $scope.mrw_name = MeHistoryOrderDel.getOrderDetailName();
      $scope.wineList = MeHistoryOrderDel.getOrdrWineList();
    });
    //改变数字的显示，添加逗号分割
    $scope.getFormatNum = function (num) {
      return formatNum(num);
    };
    // 返回上一页
    $scope.goback = function(){
      History.goback();
    };

    // 修改选中状态
    $scope.changeSelectState = function (index) {
      if($scope.wineList[index].selected){
        $scope.selectAllWine = false;
      }
      $scope.wineList[index].selected = !$scope.wineList[index].selected;
    };

    //点击全选
    $scope.selectAllWines = function () {
      $scope.selectAllWine = !$scope.selectAllWine;
      if ($scope.selectAllWine) {
        //将所有酒款都选中
        for (var i = 0; i < $scope.wineList.length; i++) {
          $scope.wineList[i].selected = true;
        }
      } else {
        //将所有酒款都去掉选中
        for (var j = 0; j < $scope.wineList.length; j++) {
          $scope.wineList[j].selected = false;
        }
      }
    };
    // 删除选中的酒款
    $scope.clickDeleteWineInOrder = function () {
      //list保存的为没有被选择的选项
      var updateList = [];
      for(var i=0;i<$scope.wineList.length;i++){
        if(!$scope.wineList[i].selected){
          updateList.push($scope.wineList[i]);
        }
      }
      //判断list是否为空
      if(updateList.length == $scope.wineList.length){
        Toast.showMessage("请选择要删除的酒款");
      }else{
        // 弹出确定窗口
        var confirmPopup = $ionicPopup.confirm({
          template: '<p style="text-align: center;font-size:16px">您确定要删除这些酒款么？</p>',
          cancelText: '取消',
          okText: '确定',
          okType: 'hd-red-btn',
          cancelType: 'cancel-esc-btn'
        });
        confirmPopup.then(function (res) {
          if (res) {
            //删除对应的酒款
            MeHistoryOrderDel.deleteWineFromOrder($scope.mrw_id,updateList);
          }
        });
      }
    };

    $scope.$on('Me.deleteWineFromOrder', function () {
      var result = MeHistoryOrderDel.getDeleteWineFromOrderJson();
      if(result.historyStatus){
        //跳转到历史酒单详情界面
        $state.go('history-order-detail');
      }else{
        /*$ionicLoading.show({
          template: result.historyMsg,
          duration: 2000
        });*/
        Toast.showMessage("result.historyMsg");
      }
    });



  });
